/**
*  Group: Y1
*  This is the Collison Manager class that will monitor all ships and bullet for collisions
*  @author Venkat Patnala
*/
package lightspeed.Backend;
import java.util.*;

public class CollisionManager {
	/**
	* the range/shield of the ship
	*/
	public static short SHIP_RANGE = 20;
	/**
	* the range of the bullet
	*/
	public static short BULLET_RANGE = 5;
	/**
	 * the range of the flag
	 */
	public static short FLAG_RANGE = 30;
	
	private int numShips = 0, numBullets = 0;
	private ArrayList ships;
	private ArrayList bullets;
	/**
	* constructor: creates a CollisonManager instance
	* Invoking the constructor will initiate two arraylists that will keep track of all the bullets and the ships in gameplay.
	* @param ships Arraylisit of all the ships in gameplay
	* @param ships Arraylisit of all the bullets in gameplay
	*/
	public CollisionManager(ArrayList ships, ArrayList bullets) {
		this.ships = ships;
		this.bullets = bullets;
	}
    /**
	* Adds a new ship to the gameplay
	* @param newShip a Ship object which will be added to the ship arraylist since its now in gameplay
	*/
	public void addShip(Ship newShip) {
		ships.add(newShip);
	}
    /**
	* Adds a new bullet to the gameplay
	* @param newBullet a bullet object which will be added to the bullet arraylist since its now in gameplay
	*/
	public void addBullet(Bullet newBullet) {
		bullets.add(newBullet);
	}
    /**
	* Updates the ship gameplay after a collision
	* @return Returns a ship that has a collosion with a bullet, else returns a NULL
	*/
	public Ship update(){	
		for (int s = 1; s <= ships.size(); s++){
			Ship curr = (Ship)ships.get(s-1);
			if (curr.team == 0)
			{
				Flag flag = (Flag) GameManager.flagRed;
				Flag flagEnemy = (Flag) GameManager.flagBlue;
				if (flag.getShip() == null && CollisionDetect.detect(flag.getX(), flag.getY(), (float)FLAG_RANGE, (float)curr.getX(), (float)curr.getY(), (float)SHIP_RANGE))
				{
					flag.setShip(curr);
				}
				if (flag.getShip() == curr && CollisionDetect.detect(flagEnemy.startX, flagEnemy.startY, (float)FLAG_RANGE, (float)curr.getX(), (float)curr.getY(), (float)SHIP_RANGE))
				{
					GameManager.scoreBlue++;
					flag.reset();
				}
			}
			if (curr.team == 1)
			{
				Flag flag = (Flag) GameManager.flagBlue;
				Flag flagEnemy = (Flag) GameManager.flagRed;
				if (flag.getShip() == null && CollisionDetect.detect(flag.getX(), flag.getY(), (float)FLAG_RANGE, (float)curr.getX(), (float)curr.getY(), (float)SHIP_RANGE))
				{	
					flag.setShip(curr);
				}
				if (flag.getShip() == curr && CollisionDetect.detect(flagEnemy.startX, flagEnemy.startY, (float)FLAG_RANGE, (float)curr.getX(), (float)curr.getY(), (float)SHIP_RANGE))
				{
					GameManager.scoreRed++;
					flag.reset();
				}
				
			}
			//curr.updatePosition();
			for (int b = 1; b <= bullets.size(); b++){
				bullets.trimToSize();
				Bullet currb = (Bullet)bullets.get(b-1);
				/*if(s==1)
				{
					if(currb.updatePosition() == false)
					{					
						bullets.remove(currb);
					}
				}*/
				if(currb.team != curr.team){
					if(CollisionDetect.detect((float)curr.getX(), (float)curr.getY(), (float)SHIP_RANGE, (float)currb.getX(), (float)currb.getY(), (float)BULLET_RANGE)){
						bullets.remove(currb);
						if(curr.incrementDamage(1)==false){
							/*System.out.println("SHIP DESTROYED, WAIT FOR RESPAWN");
							try {
					       		Thread.currentThread().sleep(3000);
    						}
							catch (InterruptedException e) {
								e.printStackTrace();
    						}*/
							return curr; 
						}
						
					}
				}
			}
		}
		return null;
	}
	
	public void updateBullets()
	{
		for (int b = 0; b < bullets.size(); ++b)
		{
			if(((Bullet)bullets.get(b)).updatePosition() == false)
			{
				bullets.remove(b);
			}
		}
	}

}
